{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cf7ab896",
   "metadata": {},
   "source": [
    "# 目录"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3be9695e",
   "metadata": {},
   "source": [
    "产生目录的代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9ea23453",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import urllib.request\n",
    "\n",
    "import nbformat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ae8a6d1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "root_path = Path(\".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2785fa3",
   "metadata": {},
   "source": [
    "遍历文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ffe08ea9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ch00-Python2与3的核心区别/00-01Python2和3的核心区别.ipynb\n",
      "ch01-Python介绍/01-01Python简介.ipynb\n",
      "ch01-Python介绍/01-02IPython解释器.ipynb\n",
      "ch01-Python介绍/01-03Jupyter Notebook.ipynb\n",
      "ch01-Python介绍/01-04脚本模式与解释器模式.ipynb\n",
      "ch02-Python基础/02-01Python演示.ipynb\n",
      "ch02-Python基础/02-02数字.ipynb\n",
      "ch02-Python基础/02-03字符串.ipynb\n",
      "ch02-Python基础/02-04索引和切片.ipynb\n",
      "ch02-Python基础/02-05列表.ipynb\n",
      "ch02-Python基础/02-06可变与不可变类型.ipynb\n",
      "ch02-Python基础/02-07元组.ipynb\n",
      "ch02-Python基础/02-08字典.ipynb\n",
      "ch02-Python基础/02-09集合与不可变集合.ipynb\n",
      "ch02-Python基础/02-10Python赋值机制.ipynb\n",
      "ch02-Python基础/02-11判断.ipynb\n",
      "ch02-Python基础/02-12循环.ipynb\n",
      "ch02-Python基础/02-13函数.ipynb\n",
      "ch02-Python基础/02-14模块与包.ipynb\n",
      "ch02-Python基础/02-15文件读写.ipynb\n",
      "ch02-Python基础/02-16异常与警告.ipynb\n",
      "ch02-Python基础/02-17常用内置函数.ipynb\n",
      "ch03-Python进阶/03-01函数进阶.ipynb\n",
      "ch03-Python进阶/03-02迭代器与生成器.ipynb\n",
      "ch03-Python进阶/03-03装饰器.ipynb\n",
      "ch03-Python进阶/03-04上下文管理器.ipynb\n",
      "ch03-Python进阶/03-05变量作用域.ipynb\n",
      "ch04-Python面向对象编程/04-01面向对象简介.ipynb\n",
      "ch04-Python面向对象编程/04-02方法与属性.ipynb\n",
      "ch04-Python面向对象编程/04-03继承与复用.ipynb\n",
      "ch04-Python面向对象编程/04-04公有、私有、特殊以及静态方法和属性.ipynb\n",
      "ch04-Python面向对象编程/04-05多重继承.ipynb\n",
      "ch05-Python标准库/05-01模块sys：系统相关.ipynb\n",
      "ch05-Python标准库/05-02模块os：与操作系统进行交互.ipynb\n",
      "ch05-Python标准库/05-03模块re：正则表达式.ipynb\n",
      "ch05-Python标准库/05-04模块datetime：日期时间.ipynb\n",
      "ch05-Python标准库/05-05模块json：处理JSON数据.ipynb\n",
      "ch05-Python标准库/05-06模块glob：文件模式匹配.ipynb\n",
      "ch05-Python标准库/05-07模块math：数学.ipynb\n",
      "ch05-Python标准库/05-08模块random：随机数.ipynb\n",
      "ch05-Python标准库/05-09模块pathlib：路径操作.ipynb\n",
      "ch06-NumPy模块/06-01NumPy模块简介.ipynb\n",
      "ch06-NumPy模块/06-02数组基础.ipynb\n",
      "ch06-NumPy模块/06-03数组广播机制.ipynb\n",
      "ch06-NumPy模块/06-04数组操作.ipynb\n",
      "ch06-NumPy模块/06-05数组的读写.ipynb\n",
      "ch06-NumPy模块/06-06数组索引进阶.ipynb\n",
      "ch06-NumPy模块/06-07随机数组.ipynb\n",
      "ch06-NumPy模块/未命名.ipynb\n",
      "ch07-Matplotlib模块/07-01Matplotlib模块简介.ipynb\n",
      "ch07-Matplotlib模块/07-02基于函数的可视化操作.ipynb\n",
      "ch07-Matplotlib模块/07-03基于对象的可视化操作.ipynb\n",
      "ch07-Matplotlib模块/07-04图像中的文本处理.ipynb\n",
      "ch08-SciPy模块/08-01SciPy模块简介.ipynb\n",
      "ch08-SciPy模块/08-02插值模块.ipynb\n",
      "ch08-SciPy模块/08-03概率统计模块.ipynb\n",
      "ch08-SciPy模块/08-04优化模块.ipynb\n",
      "ch08-SciPy模块/08-05线性代数模块.ipynb\n",
      "ch09-Pandas模块/09-01Pandas模块简介.ipynb\n",
      "ch09-Pandas模块/09-02一维数据结构Series对象.ipynb\n",
      "ch09-Pandas模块/09-03二维数据结构DataFrame对象.ipynb\n",
      "ch09-Pandas模块/09-04Pandas对象的索引.ipynb\n",
      "ch09-Pandas模块/09-05缺失值的处理.ipynb\n",
      "ch09-Pandas模块/09-06数据的读写.ipynb\n",
      "ch10-一些好玩的应用/10-01获取必应桌面壁纸.ipynb\n"
     ]
    }
   ],
   "source": [
    "folder = None\n",
    "\n",
    "result = \"\"\"\n",
    "# 中文 Python 笔记\n",
    "\n",
    "Python 3版本的实体书：[《自学Python 编程基础、科学计算及数据分析 第2版》(李金)](https://item.jd.com/13403155.html)，京东/淘宝/当当等各平台均可购买，喜欢实体书的可以看看。\n",
    "\n",
    "默认安装了 Python 3，以及相关的第三方包 `notebook`，`ipython`， `numpy`， `scipy`，`pandas`。推荐使用 Anaconda，这个IDE集成了大部分常用的包。\n",
    "\n",
    "> life is short. use python.\n",
    "\n",
    "\n",
    "笔记内容使用 Jupyter Notebook 来展示。安装好 Python 和相应的包之后，可以在命令行下输入：\n",
    "\n",
    "```shell\n",
    "jupyter notebook\n",
    "```\n",
    "\n",
    "也可以在[NBViewer](http://nbviewer.ipython.org/github/lijin-THU/notes-python3/blob/master/ReadMe.ipynb)中查看相关内容。\n",
    "\n",
    "目录：\n",
    "\"\"\"\n",
    "\n",
    "for name in sorted(root_path.glob(\"ch*/*.ipynb\")):\n",
    "    print(name)\n",
    "    if not folder or folder != name.parent:\n",
    "        folder = name.parent\n",
    "        result += f\"- [{folder.name}]({folder.name})\\n\"\n",
    "    result += f\"\\t- [{name.stem}]({urllib.request.quote(str(name))})\\n\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b2c4d985",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"ReadMe.md\", \"w\") as f:\n",
    "    f.write(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8ca37244",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "with open(\"index.ipynb\") as f:\n",
    "    index = nbformat.read(f, nbformat.NO_CONVERT)\n",
    "    index[\"cells\"] = index[\"cells\"][0:1]\n",
    "    index[\"cells\"][0][\"source\"] = result\n",
    "\n",
    "with open(\"ReadMe.ipynb\", \"w\") as f:\n",
    "    nbformat.write(index, f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e4bba4e",
   "metadata": {},
   "source": [
    "产生静态文件的代码，需要强行将`nbconvert`降一个版本才能运行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3663d28f",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install nbconvert==5.6.1 -q -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c99075a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nbconvert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d8d84e15",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "html_files/ch00-Python2与3的核心区别/00-01Python2和3的核心区别.html\n",
      "html_files/ch01-Python介绍/01-01Python简介.html\n",
      "html_files/ch01-Python介绍/01-02IPython解释器.html\n",
      "html_files/ch01-Python介绍/01-03Jupyter Notebook.html\n",
      "html_files/ch01-Python介绍/01-04脚本模式与解释器模式.html\n",
      "html_files/ch02-Python基础/02-01Python演示.html\n",
      "html_files/ch02-Python基础/02-02数字.html\n",
      "html_files/ch02-Python基础/02-03字符串.html\n",
      "html_files/ch02-Python基础/02-04索引和切片.html\n",
      "html_files/ch02-Python基础/02-05列表.html\n",
      "html_files/ch02-Python基础/02-06可变与不可变类型.html\n",
      "html_files/ch02-Python基础/02-07元组.html\n",
      "html_files/ch02-Python基础/02-08字典.html\n",
      "html_files/ch02-Python基础/02-09集合与不可变集合.html\n",
      "html_files/ch02-Python基础/02-10Python赋值机制.html\n",
      "html_files/ch02-Python基础/02-11判断.html\n",
      "html_files/ch02-Python基础/02-12循环.html\n",
      "html_files/ch02-Python基础/02-13函数.html\n",
      "html_files/ch02-Python基础/02-14模块与包.html\n",
      "html_files/ch02-Python基础/02-15文件读写.html\n",
      "html_files/ch02-Python基础/02-16异常与警告.html\n",
      "html_files/ch02-Python基础/02-17常用内置函数.html\n",
      "html_files/ch03-Python进阶/03-01函数进阶.html\n",
      "html_files/ch03-Python进阶/03-02迭代器与生成器.html\n",
      "html_files/ch03-Python进阶/03-03装饰器.html\n",
      "html_files/ch03-Python进阶/03-04上下文管理器.html\n",
      "html_files/ch03-Python进阶/03-05变量作用域.html\n",
      "html_files/ch04-Python面向对象编程/04-01面向对象简介.html\n",
      "html_files/ch04-Python面向对象编程/04-02方法与属性.html\n",
      "html_files/ch04-Python面向对象编程/04-03继承与复用.html\n",
      "html_files/ch04-Python面向对象编程/04-04公有、私有、特殊以及静态方法和属性.html\n",
      "html_files/ch04-Python面向对象编程/04-05多重继承.html\n",
      "html_files/ch05-Python标准库/05-01模块sys：系统相关.html\n",
      "html_files/ch05-Python标准库/05-02模块os：与操作系统进行交互.html\n",
      "html_files/ch05-Python标准库/05-03模块re：正则表达式.html\n",
      "html_files/ch05-Python标准库/05-04模块datetime：日期时间.html\n",
      "html_files/ch05-Python标准库/05-05模块json：处理JSON数据.html\n",
      "html_files/ch05-Python标准库/05-06模块glob：文件模式匹配.html\n",
      "html_files/ch05-Python标准库/05-07模块math：数学.html\n",
      "html_files/ch05-Python标准库/05-08模块random：随机数.html\n",
      "html_files/ch05-Python标准库/05-09模块pathlib：路径操作.html\n",
      "html_files/ch06-NumPy模块/06-01NumPy模块简介.html\n",
      "html_files/ch06-NumPy模块/06-02数组基础.html\n",
      "html_files/ch06-NumPy模块/06-03数组广播机制.html\n",
      "html_files/ch06-NumPy模块/06-04数组操作.html\n",
      "html_files/ch06-NumPy模块/06-05数组的读写.html\n",
      "html_files/ch06-NumPy模块/06-06数组索引进阶.html\n",
      "html_files/ch06-NumPy模块/06-07随机数组.html\n",
      "html_files/ch07-Matplotlib模块/07-01Matplotlib模块简介.html\n",
      "html_files/ch07-Matplotlib模块/07-02基于函数的可视化操作.html\n",
      "html_files/ch07-Matplotlib模块/07-03基于对象的可视化操作.html\n",
      "html_files/ch07-Matplotlib模块/07-04图像中的文本处理.html\n",
      "html_files/ch08-SciPy模块/08-01SciPy模块简介.html\n",
      "html_files/ch08-SciPy模块/08-02插值模块.html\n",
      "html_files/ch08-SciPy模块/08-03概率统计模块.html\n",
      "html_files/ch08-SciPy模块/08-04优化模块.html\n",
      "html_files/ch08-SciPy模块/08-05线性代数模块.html\n",
      "html_files/ch09-Pandas模块/09-01Pandas模块简介.html\n",
      "html_files/ch09-Pandas模块/09-02一维数据结构Series对象.html\n",
      "html_files/ch09-Pandas模块/09-03二维数据结构DataFrame对象.html\n",
      "html_files/ch09-Pandas模块/09-04Pandas对象的索引.html\n",
      "html_files/ch09-Pandas模块/09-05缺失值的处理.html\n",
      "html_files/ch09-Pandas模块/09-06数据的读写.html\n",
      "html_files/ch10-一些好玩的应用/10-01获取必应桌面壁纸.html\n",
      "html_files/ReadMe.html\n"
     ]
    }
   ],
   "source": [
    "html_path = root_path / \"html_files\"\n",
    "\n",
    "def convert_file(name, exporter, to_suffix, file_path):\n",
    "    doc = nbconvert.export(nbconvert.get_exporter(exporter), name)[0]\n",
    "    if \"ReadMe\" in str(name):\n",
    "        doc = doc.replace(\"ipynb\", \"html\")\n",
    "    file_name = file_path / name.with_suffix(to_suffix)\n",
    "    print(file_name)\n",
    "    if not file_name.parent.exists():\n",
    "        file_name.parent.mkdir(parents=True)\n",
    "    with file_name.open(mode = \"w\") as f:\n",
    "        f.write(doc)\n",
    "\n",
    "for name in sorted(root_path.glob(\"ch*/*.ipynb\")) + [Path(\"ReadMe.ipynb\")]:\n",
    "    convert_file(name, \"html\", \".html\", html_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "39877748",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install nbconvert -U -q -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "174845f9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
